package cn.com.surker.workcon.service.siwei;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.control.entity.ControlExpendBatch;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.system.IsAutoEnum;
import cn.com.surker.lab.entity.LabExpendBatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.MathUtils;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.PieceService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 上海思维盘信息 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class PieceServiceImpl extends GKJDBCService implements PieceService {

	/**
	 * 根据工控机的发货单,查询工控机的盘信息
	 *
	 * @param prodId   工控机的发货单id
	 * @param beSerial 起始盘号(第几盘,不包括该盘本身,可以为空)
	 * @return
	 */
	public List<LabExpendBatch> findByProdId(Long lineId, Long invoiceId, String prodId, Integer beSerial, Date createDate) {
		if (lineId == null || StringUtils.isBlank(prodId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询sql,上海思维的没有单独的盘数据,只有每车数据中有一个盘方量的组合数据
		String querySql = "select FGpfs,FScrq,FCcsj from tjlb where FNo = " + prodId + ";";
		List<Map<String, Object>> pieceList = lineJdbcTemp.queryForList(querySql);
		if (pieceList == null || pieceList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<LabExpendBatch> batchList = new ArrayList<>();
		Map<String, Object> map = pieceList.get(0);
		//	生产时间
		////	生产日期
		String fscrq = String.valueOf(map.get("FScrq"));
		fscrq = fscrq.replaceAll("\\s+", "");
		////	出场时间
		String fccsj = String.valueOf(map.get("FCcsj"));
		String prodTimE = fscrq + " " + fccsj + ":00";
		Date prodTim = DateUtil.convertDate(prodTimE);
		//	取出每盘方量
		String fgpfs = String.valueOf(map.get("FGpfs"));
		if (StringUtils.isBlank(fgpfs)) {
			fgpfs = "00";
		}
		//	格式化字符串数据,每两位为一盘
		int length = fgpfs.length();
		int batchNum = 1;
		for (int i = 0; i < length; i += 2) {
			// 从字符串中截取两位字符，如果不足两位则补零
			String pflStr = fgpfs.substring(i, Math.min(i + 2, length));
			if (pflStr.length() == 1) {
				pflStr += "0"; // 补零
			}
			double pfl = Double.valueOf(pflStr);
			pfl = MathUtils.subtract(pfl, 10d);
			//	创建盘数据
			LabExpendBatch erpBatch = new LabExpendBatch();
			erpBatch.setInvoiceId(invoiceId);
			erpBatch.setLineId(lineId);
			erpBatch.setBatchStere(pfl);
			erpBatch.setBatchTime(prodTim);
			erpBatch.setConBatchId(String.valueOf(batchNum));
			erpBatch.setConBatchNum(batchNum);
			erpBatch.setType(IsAutoEnum.ZD.getCode());
			erpBatch.setRemark("");
			erpBatch.setCreator(bcsCreater);
			erpBatch.setCreateTime(createDate);
			batchList.add(erpBatch);
			batchNum++;
		}
		return batchList;
	}

	@Override
	public List<ControlExpendBatch> findByProdId(Long lineId, Long erpInvId, String prodId, Date createDate) {
		//	转换数据
		if (lineId == null || StringUtils.isBlank(prodId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询sql,上海思维的没有单独的盘数据,只有每车数据中有一个盘方量的组合数据
		String querySql = "select FGpfs,FScrq,FCcsj from tjlb where FNo = " + prodId + ";";
		List<Map<String, Object>> pieceList = lineJdbcTemp.queryForList(querySql);
		if (pieceList == null || pieceList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<ControlExpendBatch> batchList = new ArrayList<>();
		Map<String, Object> map = pieceList.get(0);
		//	生产时间
		////	生产日期
		String fscrq = String.valueOf(map.get("FScrq"));
		fscrq = fscrq.replaceAll("\\s+", "");
		////	出场时间
		String fccsj = String.valueOf(map.get("FCcsj"));
		String prodTimE = fscrq + " " + fccsj + ":00";
		Date prodTim = DateUtil.convertDate(prodTimE);
		//	取出每盘方量
		String fgpfs = String.valueOf(map.get("FGpfs"));
		if (StringUtils.isBlank(fgpfs)) {
			fgpfs = "00";
		}
		//	格式化字符串数据,每两位为一盘
		int length = fgpfs.length();
		int batchNum = 1;
		for (int i = 0; i < length; i += 2) {
			// 从字符串中截取两位字符，如果不足两位则补零
			String pflStr = fgpfs.substring(i, Math.min(i + 2, length));
			if (pflStr.length() == 1) {
				pflStr += "0"; // 补零
			}
			double pfl = Double.valueOf(pflStr);
			pfl = MathUtils.subtract(pfl, 10d);
			//	创建盘数据
			ControlExpendBatch swBatch = new ControlExpendBatch();
			swBatch.setLineId(lineId);
			swBatch.setErpInvoiceId(erpInvId);
			swBatch.setBatchStere(pfl);
			swBatch.setBatchTime(prodTim);
			swBatch.setConBatchNo(String.valueOf(batchNum));
			swBatch.setBatchNum(batchNum);
			swBatch.setType(IsAutoEnum.ZD.getCode());
			swBatch.setRemark("");
			swBatch.setStatus(StateEnum.ENABLED.getCode());
			swBatch.setCreator(bcsCreater);
			swBatch.setCreateTime(createDate);
			batchList.add(swBatch);
			batchNum++;
		}
		return batchList;

	}

}
